import { Injectable } from '@nestjs/common';
import { Dataset } from '@prisma/client';
import { PrismaService } from 'src/common/db/prisma.service';
import { QdrantService } from '../qdrant/qdrant.service';
const { v4: uuidv4 } = require('uuid');

@Injectable()
export class DatasetService {
    constructor(private readonly prisma: PrismaService,
        private readonly qdrant: QdrantService) { }

    create(dataset: Dataset) {
        const uuid = uuidv4();
        dataset.collection_name = uuid;
        this.qdrant.client.createCollection(uuid, {
            vectors: {
                size: 768,
                distance: 'Cosine',
            },
        });

        return this.prisma.dataset.create({
            data: dataset,
        });
    }

    delete(id: number) {
        return this.prisma.dataset.delete({
            where: {
                id: id,
            },
        });
    }

    update(id: number, data: any) {
        return this.prisma.dataset.update({
            where: {
                id: id,
            },
            data: data,
        });
    }

    findOne(id: number) {
        return this.prisma.dataset.findUnique({
            where: {
                id: id,
            },
        });
    }

    findAll() {
        return this.prisma.dataset.findMany();
    }

    findByUser(userId: string) {
        return this.prisma.dataset.findMany({
            where: {
                user_id: userId,
            },
        });
    }
}
